package com.company.biz.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.biz.product.mapper.SupplierDao;
import com.company.biz.product.service.SupplierService;
import com.company.biz.user.mapper.UserDao;
import com.company.product.entity.SupplierEntity;
import com.company.product.req.SupplierInfoReq;
import com.company.product.req.SupplierListPageReq;
import com.company.product.resp.SupplierInfoResp;
import com.company.common.enums.TableNamePrefixEnum;
import com.company.common.exception.BizException;
import com.company.common.util.DirectFieldFiller;
import com.company.common.util.IdentifierGeneratorUtil;
import com.company.user.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierDao, SupplierEntity> implements SupplierService{
    @Resource
    private UserDao userDao;

    @Override
    public Page<SupplierInfoResp> getSupplierListPage(SupplierListPageReq req) {
        LambdaQueryWrapper<SupplierEntity> queryWrapper = new QueryWrapper<SupplierEntity>()
                .lambda().eq(SupplierEntity::getIsDel, "n");
        queryWrapper.like(StringUtils.isNotBlank(req.getSupplierBrand()),
                SupplierEntity::getSupplierBrand,req.getSupplierBrand());
        queryWrapper.like(StringUtils.isNotBlank(req.getSupplierName()),
                SupplierEntity::getSupplierName,req.getSupplierName());
        Page<SupplierEntity> page = page(new Page<>(req.getPage(),
                req.getSize()), queryWrapper);
        Page<SupplierInfoResp> supplierInfoRespPage= new Page<>();
        supplierInfoRespPage.setTotal(page.getTotal());
        supplierInfoRespPage.setCurrent(page.getCurrent());
        supplierInfoRespPage.setPages(page.getPages());
        List<SupplierEntity> records = page.getRecords();
        List<SupplierInfoResp>  supplierInfoRespList=new ArrayList<>();
        for (SupplierEntity record : records) {
            SupplierInfoResp supplierInfoResp = new SupplierInfoResp();
            record.copyTo(supplierInfoResp);
            supplierInfoRespList.add(supplierInfoResp);
        }
        //补充操作人姓名
        supplementUserName(supplierInfoRespList);
        supplierInfoRespPage.setRecords(supplierInfoRespList);
        return supplierInfoRespPage;
    }

    @Override
    public boolean addSupplier(SupplierInfoReq req, UserEntity userEntity) {
        SupplierEntity supplierEntity = new SupplierEntity();
        supplierEntity.setSupplierId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.TBSUPPLIER));
        req.copyTo(supplierEntity);
        supplierEntity.setUserId(userEntity.getUserId());
        supplierEntity.setIsDel("n");
        return save(supplierEntity);
    }

    @Override
    public SupplierInfoResp getSupplierBySupplierId(String supplierId) {
        SupplierEntity supplierEntity = getById(supplierId);
        if(ObjectUtil.isEmpty(supplierEntity)){
            throw new BizException("","你查找的该供应商不存在");
        }
        SupplierInfoResp supplierInfoResp = new SupplierInfoResp();
        supplierEntity.copyTo(supplierInfoResp);
        UserEntity userEntity = userDao.selectById(supplierInfoResp.getUserId());
        if(ObjectUtil.isNotEmpty(userEntity)){
            supplierInfoResp.setUserName(userEntity.getUserName());
        }
        return supplierInfoResp;
    }

    @Override
    public boolean updateSupplierBySupplierId(SupplierInfoReq req,UserEntity userEntity) {
        LambdaUpdateWrapper<SupplierEntity> supplierEntityLambdaUpdateWrapper = new UpdateWrapper<SupplierEntity>().lambda();
        supplierEntityLambdaUpdateWrapper.set(SupplierEntity::getUserId,userEntity.getUserId());
        supplierEntityLambdaUpdateWrapper.set(SupplierEntity::getSupplierBrand,req.getSupplierBrand());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getSupplierBrand()),SupplierEntity::getSupplierBrand,req.getSupplierBrand());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getSupplierName()),SupplierEntity::getSupplierName,req.getSupplierName());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getSupplierNumber()),SupplierEntity::getSupplierNumber,req.getSupplierNumber());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getDistrict()),SupplierEntity::getDistrict,req.getDistrict());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getRecord()),SupplierEntity::getRecord,req.getRecord());
        supplierEntityLambdaUpdateWrapper.set(StringUtils.isNotBlank(req.getRemarks()),SupplierEntity::getRemarks,req.getRemarks());
        supplierEntityLambdaUpdateWrapper.set(ObjectUtil.isNotEmpty(req.getGender()),SupplierEntity::getGender,req.getGender());
        supplierEntityLambdaUpdateWrapper.set(ObjectUtil.isNotEmpty(req.getSupplierStatus()),SupplierEntity::getGender,req.getSupplierStatus());
        supplierEntityLambdaUpdateWrapper.eq(SupplierEntity::getSupplierId, req.getSupplierId());
        return update(supplierEntityLambdaUpdateWrapper);
    }

    @Override
    public boolean deleteSupplierBySupplierIds(List<String> supplierIds,UserEntity userEntity) {
        LambdaUpdateWrapper<SupplierEntity> supplierEntityLambdaUpdateWrapper = new UpdateWrapper<SupplierEntity>().lambda();
        supplierEntityLambdaUpdateWrapper.set(SupplierEntity::getIsDel,"y");
        supplierEntityLambdaUpdateWrapper.set(SupplierEntity::getUserId,userEntity.getUserId());
        supplierEntityLambdaUpdateWrapper.in(SupplierEntity::getSupplierId, supplierIds);
        return update(supplierEntityLambdaUpdateWrapper);
    }

    //补充操作人的姓名
    public void supplementUserName(List<SupplierInfoResp> clientInfoRespList) {
        //补充部门名
        DirectFieldFiller<String, UserEntity, SupplierInfoResp> UserNameFiller = new DirectFieldFiller<String, UserEntity, SupplierInfoResp>() {
            @Override
            protected String whichIdNeed(SupplierInfoResp item) {
                return item.getUserId();
            }

            @Override
            public String getIdFromValue(UserEntity obj) {
                return obj.getUserId();
            }

            @Override
            public List<UserEntity> loadFromDB(List<String> ids) {
                return userDao.selectList(new QueryWrapper<UserEntity>().lambda().in(UserEntity::getUserId, ids));
            }

            @Override
            public void fillField(SupplierInfoResp obj, UserEntity val) {
                obj.setUserName(val.getUserName());
            }
        };
        UserNameFiller.fill(clientInfoRespList);
    }
}
